package com.arnold.test.demos.fileStream

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * Created by arnold.zhu on 2017/8/28.
  */
object FileWordCount2 {

  def main(args: Array[String]): Unit = {
    val ssc = new StreamingContext(new SparkConf(), Seconds(3))

    // Create a DStream that will connect to serverIP:serverPort, like localhost:9999
    val lines = ssc.socketTextStream("hadoop000", 9999)

    // Split each line into words
    val words = lines.flatMap(_.split(" "))


    // Count each word in each batch
    val pairs = words.map(word => (word, 1))
    val wordCounts = pairs.reduceByKey(_ + _)

    // Print a few of the counts to the console
    wordCounts.print()
    ssc.start() // Start the computation
    ssc.awaitTermination() // Wait for the computation to terminate

  }

}
